layui.config({
    base : "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index','form','jquery', 'table'],function(){

    var form   = layui.form,
        table  = layui.table,
        admin  = layui.admin;

    /**
     * 初始化 分润设置
     */

    initEnterpriseProfitSelect();

    table.render({
         elem: '#enterprise_User_table'
        ,url: '/enterprise_user/queryData'
        ,method:"post"
        ,height: 500
        ,limit:50							//显示数目
        ,limits:[50,100]
        ,cols: [[
            {field:'id', title: 'id',width:100, "align":"center"}
            ,{field:'userName',width:200, title: '用户名',"align":"center"}
            ,{field:'mobile',width:150, title: '手机号码',"align":"center"}
            ,{field:'profitName',width:200, title: '分润补贴名',"align":"center",templet:'<span>{{d.profitInfo.name}}</span>'}
            ,{field:'consumeProfit',width:200, title: '分润补贴值',"align":"center",templet:
                    function (d) {

                        var consumeProfit= d.profitInfo.consumeProfit;
                        return consumeProfit==null?0:new Number(consumeProfit.toString()).mul(new Number(100));
                    }}
            ,{field:'createdTime',width:200, title: '注册时间',"align":"center"}
            ,{field:'OPT', width:400, title: '操作',"align":"center",templet:function (d) {
                    return     '<a  href="#" class="layui-btn layui-btn-xs" lay-event="modify_enterpriseUser" >编辑</a>' +
                        '<a  href="#" class="layui-btn layui-btn-xs" lay-event="modify_enterpriseTer" >设备列表</a>' +
                        '<a  href="#" class="layui-btn layui-btn-xs" lay-event="enterprise_info" >商家配置</a>' +
                        '<a  href="#" class="layui-btn layui-btn-xs" lay-event="manager_enterpriseInfo" >进件</a>'+
                        '<a  href="#" class="layui-btn layui-btn-xs" lay-event="applyMentList">进件列表</a>';
                }}
        ]]
        ,request:{
             pageName: 'pageNo' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where:{
            userName:$(".queryTable input[name='userName']").val(),
            mobile:$(".queryTable input[name='mobile']").val()
        }
        ,id: 'enterprise_User_table'
        ,page: true
    });


    table.on('tool(enterprise_User_table)', function(obj){
        var table_event = obj.event;

        var data = obj.data;

        if(table_event=="modify_enterpriseUser"){
            $("#add_enterpriseUser_form input[name='userName']").attr("disabled","true");
            $("#add_enterpriseUser_form input[name='mobile']").attr("disabled","true");

            admin.popup({
                title: '编辑'
                ,shade: 0
                ,anim: -1
                ,offset:"20px"
                ,area: ['80%', '500px']
                ,skin: 'layui-anim layui-anim-upbit'
                ,content:$("#add_enterpriseUser_win")
                ,end:function () {
                    table.reload('enterprise_User_table', {
                        page: {
                            curr: 1 //重新从第 1 页开始
                        }
                        ,where: {
                        }
                    });
                }
            });

            form.val('add_enterpriseUser_form', {
                "id":data.id,
                 "profitId":data.profitId,
                 "userName":data.userName,
                 "mobile": data.mobile
            })

        }else if(table_event=="modify_enterpriseTer") {

            var userId = data.id;

            layer.open({
                type: 2,
                title: '设备列表',
                shadeClose:true,
                resize:false,
                area: ['900px', '500px'],
                content: '/agency_mch_terminal/list?userId='+userId //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            });

        }else if(table_event=="enterprise_info") {

            var userId = data.id;

            layer.open({
                type: 2,
                title: '商家配置',
                shadeClose:true,
                resize:false,
                area: ['900px', '500px'],
                content: '/enterprise_info/index?mchUserId='+userId //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            });

        }else if(table_event=="manager_enterpriseInfo") {
            var userId = data.id;

            layer.open({
                type: 2,
                shade: 0.6,
                title: '商户进件',
                shadeClose:true,
                resize:false,
                area: ['90%', '90%'],
                content: '/applyment/index?mchUserId='+userId //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            });
        }else  if(table_event=="applyMentList"){

            var userId = data.id;

            layer.open({
                type: 2,
                shade: 0.6,
                title: '商户进件列表',
                shadeClose:true,
                resize:false,
                area: ['90%', '90%'],
                content: '/mch_applyment/list?mchUserId='+userId //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
            });

        }

    });

    $(".search_btn").click(function () {

        table.reload('enterprise_User_table', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            ,where: {
                userName:$(".queryTable input[name='userName']").val(),
                mobile:$(".queryTable input[name='mobile']").val()
            }
        });

    });

    //添加商户
    $(".enterpriseUserAdd_btn").click(function(){

        $("#add_enterpriseUser_form")[0].reset();
        $("#add_enterpriseUser_form input[name='userName']").removeAttr("disabled");
        $("#add_enterpriseUser_form input[name='mobile']").removeAttr("disabled");

        /**
         * 初始化 等级下拉
         */

        admin.popup({
            title: '编辑'
            ,shade: 0
            ,anim: -1
            ,offset:"20px"
            ,area: ['80%', '500px']
            ,skin: 'layui-anim layui-anim-upbit'
            ,content:$("#add_enterpriseUser_win")
            ,end:function () {
                table.reload('enterprise_User_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {

                    }
                });
            }
        });
        form.val('add_enterpriseUser_form', {
            "id":""
        })

    });


    form.on('submit(addenterpriseUserOk)',function (data) {

        layer.msg('用户名和手机号码一经注册,不可更改。如果是注册用户请仔细核对信息，确认提交？', {
            time: 0, //20s后自动关闭
            id: 'LAY_layuipro',
            shade: 0.8,
            btnAlign: 'c',
            btn: ['确定', '取消'],
            yes:function () {

                $.ajax({
                    url:"/enterprise_user/edit",
                    data:data.field,
                    type:"post",
                    success:function (result) {
                        if (result.code == 1){
                            layer.msg("操作成功",{
                                offset: '32%'
                                ,icon: 1
                                ,time: 0
                                ,btn: '知道了'
                                ,btnAlign:"c"
                                ,shade:0.4
                                ,yes:function () {
                                    table.reload('enterprise_User_table', {
                                    });
                                    layer.closeAll();
                                }
                            })
                        }else{
                            layer.msg(result.message,{
                                offset: '32%'
                                ,icon: 2
                                ,time: 3000
                            },function () {
                               // layer.closeAll();
                            })
                        }

                    }
                });
            }
            }
        );


        return false;
    });

});


/**
 * 动态获取等级
 */
function initEnterpriseProfitSelect() {
    $.post(
        "/enterprise_profit/queryData",
        {},
        function (result) {

            if(result.code==1){

                $('#add_enterpriseUser_form select[name="profitId"] option').remove();
                $.each(result.data, function (index, item) {
                    $('#add_enterpriseUser_form select[name="profitId"]').append(new Option(item.name, item.id));// 下拉菜单里添加元素
                });

            }else {
                layer.msg(result.message,{
                    offset: '32%'
                    ,icon: 2
                    ,time: 3000
                },function () {
                    layer.closeAll();
                })
            }
        }
    );
};



